package de.fhhannover.inform.itteam.snmpnetviz;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Pattern;

public class HostEntry {
	private HostEntry mParent;

	private int mVlan;
	private String mIpAddress;
	private String mName;
	private String mDomain;
	private String mMacAddress;
	private int mPort;
	private int mPortIndex;
	private String mPortDescription;
	private boolean mIsSwitch;
	private ArrayList<HostEntry> mChildren = new ArrayList<HostEntry>();
	private HashMap<Integer, HostEntry> switchesForPortTable=new HashMap<Integer, HostEntry>();
	
	public HostEntry(String ip) {
		this(null, 1, ip, null, 0, 0, null,true);
	}
	
	public HostEntry(HostEntry parent, int vlan, String ip, String mac, int  port, int portIndex, String PortDesc) {
		this(parent, vlan, ip, mac, port, portIndex, PortDesc, false);
	}
	
	public HostEntry(HostEntry parent, int vlan, String ip, String mac, int  port, int portIndex, String PortDesc, boolean isSwitch) {
		mParent=parent;
		mVlan=vlan;
		mIpAddress=ip;
		mMacAddress=mac;
		mPort=port;
		mPortIndex=portIndex;
		mPortDescription=PortDesc;
		mIsSwitch=isSwitch;
		
		try {
			final Pattern IP_PATTERN = Pattern.compile("(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])");
			
			String label=InetAddress.getByName(mIpAddress).getHostName();
			if (!IP_PATTERN.matcher(label).matches()) {
				int firstSeparatorIndex = label.indexOf(".");
				if (firstSeparatorIndex>=0) {
					mName = label.substring(0, firstSeparatorIndex);
					mDomain = label.substring(firstSeparatorIndex+1);
				}
			}
			else {
				mName=label;
				mDomain="";
			}
		} catch (UnknownHostException e) {
			mName="";
			mDomain="";
		}
		
	}
	
	/**
	 * @return the parent
	 */
	public HostEntry getParent() {
		return mParent;
	}

	/**
	 * @return the vlan
	 */
	public int getVlan() {
		return mVlan;
	}

	/**
	 * @return the ipAddress
	 */
	public String getIpAddress() {
		return mIpAddress;
	}
	
	/**
	 * @parameter ip the ipAddress to set
	 */
	public void setIpAddress(String ip) {
		mIpAddress = ip;
	}

	/**
	 * @return the macAddress
	 */
	public String getMacAddress() {
		return mMacAddress;
	}

	/**
	 * @return the port
	 */
	public int getPort() {
		return mPort;
	}

	/**
	 * @return the portIndex
	 */
	public int getPortIndex() {
		return mPortIndex;
	}

	/**
	 * @return the portDescription
	 */
	public String getPortDescription() {
		return mPortDescription;
	}

	/**
	 * @param vlan the vlan to set
	 */
	public void setVlan(int vlan) {
		mVlan = vlan;
	}

	/**
	 * @param macAddress the macAddress to set
	 */
	public void setMacAddress(String macAddress) {
		mMacAddress = macAddress;
	}

	/**
	 * @param port the port to set
	 */
	public void setPort(int port) {
		mPort = port;
	}

	/**
	 * @param portIndex the portIndex to set
	 */
	public void setPortIndex(int portIndex) {
		mPortIndex = portIndex;
	}

	/**
	 * @param portDescription the portDescription to set
	 */
	public void setPortDescription(String portDescription) {
		mPortDescription = portDescription;
	}

	/**
	 * @return the isSwitch
	 */
	public boolean isSwitch() {
		return mIsSwitch;
	}

	/**
	 * @param isSwitch the isSwitch to set
	 */
	public void setIsSwitch(boolean isSwitch) {
		mIsSwitch = isSwitch;
	}

	/**
	 * @return the children
	 */
	public Iterator<HostEntry> getChildren() {
		return mChildren.iterator();
	}

	/**
	 * @param children the children to set
	 */
	public void addChildren(HostEntry children) {
		if (children!=null && !this.getIpAddress().equals(children.getIpAddress())) {
			mChildren.add(children);
			if (children.isSwitch()) {
				switchesForPortTable.put(children.getPort(), children);
			}
		}
	}
	
	public HostEntry getSwitchForPort(int port) {
		return switchesForPortTable.get(new Integer(port));
	}
	
	/**
	 * @return true if this HostEntry has Children
	 */
	public boolean hasChildren() {
		return !mChildren.isEmpty();
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return mName;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		mName = name;
	}

	/**
	 * @return the domain
	 */
	public String getDomain() {
		return mDomain;
	}

	/**
	 * @param domain the domain to set
	 */
	public void setDomain(String domain) {
		mDomain = domain;
	}

	public String toString() {
		return "Vlan: "+mVlan+"\t IP: "+mIpAddress+"\t MAC: "+mMacAddress+"\t Port: "+mPortIndex+" ("+mPortDescription+")";
	}
}
